package com.orm.analysis.service.impl;


import com.orm.analysis.dao.impl.*;
import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/12/13
 * Time: 7:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class CustOrderService implements GenericService<CustOrder> {


    private static Logger log = Logger.getLogger(CustOrderService.class);

    private String orderDataFielDir;

    private CustOrderDAO dao;

    private CustomerDAO customerDAO;

    private SalesmanDAO salesmanDAO;

    private PaymentDAO paymentDAO;

    private AddressDAO addressDAO;

    private ItemDAO itemDAO;


    public String getOrderDataFielDir() {
        return orderDataFielDir;
    }

    public void setOrderDataFielDir(String orderDataFielDir) {
        this.orderDataFielDir = orderDataFielDir;
    }

    public CustOrderDAO getDao() {
        return dao;
    }

    public void setDao(CustOrderDAO dao) {
        this.dao = dao;
    }

    public CustomerDAO getCustomerDAO() {
        return customerDAO;
    }

    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    public SalesmanDAO getSalesmanDAO() {
        return salesmanDAO;
    }

    public void setSalesmanDAO(SalesmanDAO salesmanDAO) {
        this.salesmanDAO = salesmanDAO;
    }

    public PaymentDAO getPaymentDAO() {
        return paymentDAO;
    }

    public void setPaymentDAO(PaymentDAO paymentDAO) {
        this.paymentDAO = paymentDAO;
    }

    public AddressDAO getAddressDAO() {
        return addressDAO;
    }

    public void setAddressDAO( AddressDAO addressDAO) {
        this.addressDAO = addressDAO;
    }

    public ItemDAO getItemDAO() {
        return itemDAO;
    }

    public void setItemDAO(ItemDAO itemDAO) {
        this.itemDAO = itemDAO;
    }

    public CustOrder create(CustOrder entity) {
        dao.save(entity);
        return entity;
    }


    public void delete(Object id) {

        dao.delete(id);
    }

    public CustOrder update(CustOrder entity) {
        dao.update(entity);
        return entity;
    }

    public List<CustOrder> list(int page, int size) {
        return dao.list(page,size);
    }

    public List<CustOrder> listByFields(String... fieldsNamesValues) {
        StringBuffer queryString= new StringBuffer("select a from CustOrder a ");
            return dao.listByFields(queryString,fieldsNamesValues);
    }

    public List<CustOrder> findAllWithDepth(String... fetchRelations) {
                  return dao.findAllWithDepth(CustOrder.class,fetchRelations);
              }

              public CustOrder findWithDepth(String pkFieldName, Object id, String... fetchRelations) {
                  return dao.findWithDepth(CustOrder.class,pkFieldName,id,fetchRelations);
              }



    public  int updateExecuteUpdateByQuery(String query){
        return dao.updateExecuteUpdateByQuery(query);
    }

    public CustOrder getById(CustOrder entity) {
        return dao.getById(entity.getOrderId());
    }

    public CustOrder getById(Integer id) {
        return dao.getById(id);
    }

    public void executeImproter() throws ParseException, IOException {
        try{
                log.info("File  >>" + orderDataFielDir);
                BufferedReader br = null;

                br = new BufferedReader(new FileReader(orderDataFielDir));

                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
                line = br.readLine();//Skip 1 line


                while (line != null) {

                    log.info("File  >>" + line);
                    // ORDER_ID not need,ORDER_DATE,REQUIRED_DATE , SHIPPED_DATE,COMMENTS, customer,salesman ,<OrderDetail~OrderDetail> , payment ,shippingAddress
                    CustOrder custOrder = new CustOrder();
                    String[] data = line.split("[,]");
                    custOrder.setOrderDate(GlobalConst.simpleDateFormat.parse(data[0].trim()));
                    custOrder.setRequiredDate(GlobalConst.simpleDateFormat.parse(data[1].trim()));
                    custOrder.setShippedDate(GlobalConst.simpleDateFormat.parse(data[2].trim()));
                    custOrder.setComments(data[3].trim());



                    if(data[4].trim().length()>0){
                        custOrder.setCustomer(customerDAO.getById(Integer.valueOf((data[4].trim()))));

                    }

                    if(data[5].trim().length()>0){
                        custOrder.setSalesman(salesmanDAO.getById(Integer.valueOf((data[5].trim()))));
                    }

                    List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
                    for(int i=0;i<Integer.valueOf((data[6].trim()));i++){
                        OrderDetail orderDetail = new OrderDetail();
                        orderDetail.setItem(itemDAO.getById(1));
                        orderDetail.setCustOrder(custOrder);
                        orderDetail.setQuantity( 11  );
                        orderDetailList.add(orderDetail);

                    }
                    custOrder.setOrderDetail(orderDetailList);



                    if (data[7].trim().length() > 0) {
                        custOrder.setPayment(paymentDAO.getById(Integer.valueOf((data[6].trim()))));
                    }

                    if (data[8].trim().length() > 0) {
                        custOrder.setShippingAddress(addressDAO.getById(Integer.valueOf((data[7].trim()))));
                    }



                    dao.save(custOrder);

                    line = br.readLine();

                }

                br.close();

                }catch(Exception e ){
                    e.printStackTrace();
                }
    }

    public List<Integer> getIdList() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void processTaskExecutor() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<CustOrder> getByIdList(Integer[] entityIdList) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
